package com.xpgk.xpsoft.common;

import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.common.kit.PubKit;
import com.xpgk.xpsoft.constants.XPSoftConstant;

//操作日志
public class XPSoftLog extends Thread {
	// 阻塞队列，FIFO
	private static LinkedBlockingQueue<Record> m_LinkedQueue = new LinkedBlockingQueue<Record>();
	private static Thread m_threadLog = null;
	private static boolean bStop = false;

	// 初始化，启动日志写线程
	static public void init() {
		bStop = false;
		m_LinkedQueue.clear();
		m_threadLog = new XPSoftLog();
		m_threadLog.start();
		System.out.println("log thread start.");
	}

	static public void stop2() {
		bStop = true;
	}

	public static boolean write(int userID, String name, String param, Integer type1, Integer type2, Integer type3,
			String sm, String ip) {
		if (m_threadLog == null)
			init();
		Record e = new Record();
		e.set("systemID", PubKit.ifNull(XPSoftConstant.systemID, 1)).set("gxsj", new Date()).set("userID", userID)
				.set("name", name).set("param", param);
		if (type1 != null)
			e.set("lx1", type1);
		if (type2 != null)
			e.set("lx2", type2);
		if (type3 != null)
			e.set("lx3", type3);
		if (sm != null)
			e.set("sm", sm);
		if (ip != null)
			e.set("ip", ip);
		return m_LinkedQueue.offer(e);
	}

	public static boolean write(int userID, String name, String param, Integer type1, Integer type2, String sm,
			String ip) {
		return write(userID, name, param, type1, type2, null, sm, ip);
	}

	public static boolean write(int userID, String name, String param, Integer type1, Integer type2, String sm) {
		return write(userID, name, param, type1, type2, sm, null);
	}

	public static boolean write(int userID, String name, String param, Integer type1, Integer type2) {
		return write(userID, name, param, type1, type2, null);
	}

	public static boolean write(int userID, String name, String param, Integer type1) {
		return write(userID, name, param, type1, null);
	}

	public static boolean write(int userID, String name, String param) {
		return write(userID, name, param, null);
	}

	private static boolean writeLog(Record log) {
		String jyw = HashKit.md5(log.getStr("systemID") + log.getStr("gxsj") + log.getStr("userID") + "log"
				+ log.getStr("name") + log.getStr("lx1") + log.getStr("ip"));
		log.set("jyw", jyw);
		return Db.save("FRM_LOG", log);
	}

	// 日志校验位是否正常
	public static boolean isValidJYW(Record log) {
		return true;
	}

	public void run() {
		Record record;
		while (!bStop) {
			// 队列空时，指定阻塞时间后返回，不会一直阻塞
			try {
				record = m_LinkedQueue.poll(500, TimeUnit.MILLISECONDS);
				if (record != null) {
					writeLog(record);
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if (DbKit.getConfig() == null)
					return;
			}
		}
		System.out.println("log thread stop.");
	}
}
